#include"Queue.h"
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;
}

void QueueDestroy(Queue* pq)
{
	assert(pq);
	QueueNode* cur = pq->head;
	while (cur )
	{
		QueueNode* next =cur->next;
		free(cur);
		cur = next;
	}
}

void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);
	QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newNode == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	else 
	{
		if (pq->tail == NULL)
		{
			pq->head=pq->tail = newNode;
			newNode->data = x;
		}
		else
			pq->tail = newNode;
			newNode->data = x;
	}
}

void QueuePop(Queue* pq)
{
	assert(pq);
	if (pq->head == NULL)
	{
		free(pq->tail);
	}
	else {
		QueueNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
}

int QueueSize(Queue* pq)
{
	assert(pq);
	QueueNode* cur = pq->head;
	int size = 0;
	while (cur)
	{
		QueueNode* next = cur->next;
		size++;
		cur = next;
	}
}

BOOL QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->head == pq->tail;
}

QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->tail->data;
}

QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->head->data;
}

void QueuePrint(Queue* pq)
{
	QueueNode* next = pq->head->data;
	Queue* cur = pq->head;
	while (cur)
	{
		printf("%d\t", cur->head->data);
	}
}